package main

import  "fmt"
import "strings"

/*
Example
If source = "source" and target = "target", return -1.
If source = "abcdabcdefg" and target = "bcd", return 1.
*/
var  pl = fmt.Println   // 这样写可以简化打印语句，推荐

// 判断切片是否相等
func isSameSlice(s1 []string,s2 []string)  bool{
	if len(s1)==0{
		return false
	} 
	for i := range s1 {
	   if s1[i]!=s2[i] {
		   return  false
	   }
	}
	return true
}

func strStr(source string,tag string) int {
	var  sourceSlice []string
	var  tagSlice   []string

	sourceSlice  = strings.Split(source,"")
	tagSlice     = strings.Split(tag,"")

	if len(tagSlice) == 0 && len(sourceSlice) == 0{
		return  0
	}
	if len(sourceSlice) == 0{
		return -1
	}
	if len(tagSlice) == 0{
		return 0
	}
	for  i:= range sourceSlice{
		if i > len(sourceSlice)-len(tagSlice){
			pl("可能会导致下标越界")
			return -1
		}
		if sourceSlice[i]==tagSlice[0]{
			from := i ;	to:= len(tagSlice)+i   // to 有可能下标溢出
				if isSameSlice(sourceSlice[from:to],tagSlice) == true{
					return i
				}
				}
		}
	return  -1
}
func main()  {
	//pl(strStr("","addsa"))  // 边界判断1 ，-1
	//pl(strStr("",""))  // 边界判断 ,0
	//pl(strStr("dgfd",""))  // 边界判断 ,0
	//pl(strStr("abscddf","ddfer"))  // 防止下标越界
}

// 算法时间复杂度分析 设 len(sourceSlice)== n len(tagSlice)== m

//  双重循环 最坏时间复杂度  O (n-m)*m